#include "command/sceneobjectmovecmd.h"
#include "editorscenemgr.h"
#include "scene/editorscene.h"
#include "scene/sceneobjectnavigationmap.h"
#include "sceneobjects/sceneobjectmanager.h"
#include "sceneobjects/sceneobject.h"


CSceneObjectMoveCmd::CSceneObjectMoveCmd( list<SSceneObjectMovePosInfo> *pvStartMoveInfoList, list<SSceneObjectMovePosInfo> *pvEndMoveInfoList ):
CCommand(){
    m_vStartMoveInfoList = *pvStartMoveInfoList;
    m_vEndMoveInfoList = *pvEndMoveInfoList;
}
CSceneObjectMoveCmd::~CSceneObjectMoveCmd(){

}
void CSceneObjectMoveCmd::Init(){

}
void CSceneObjectMoveCmd::Final(){

}
bool32 CSceneObjectMoveCmd::Undo(){
    CSceneObjectNavigationMap* _pSONavigationMap = m_pSceneMgr->GetSceneObjectNavigationMap();
    CSceneObjectManager* _pSceneObjectManager = m_pSceneMgr->GetSceneObjectMgr();

    for( list<SSceneObjectMovePosInfo>::iterator it = m_vStartMoveInfoList.begin();
         it != m_vStartMoveInfoList.end(); it ++ ){
        SSceneObjectMovePosInfo* _pSOMovePosInfo = &(*it);
        CSceneObject* _pSO = _pSceneObjectManager->GetSceneObject( _pSOMovePosInfo->m_nID );
        if( _pSO ){
            list<TPosition> _vPathList1;
            _pSONavigationMap->GetCellTypeListValue(_pSO->m_nCellType, 0, _pSO->m_sMapGridPos.X, _pSO->m_sMapGridPos.Y, &_vPathList1 );
            _pSONavigationMap->SetSOListValue( &_vPathList1, NULL );
            _pSOMovePosInfo->m_pSceneObject = _pSO;
        }
    }

    for( list<SSceneObjectMovePosInfo>::iterator it = m_vStartMoveInfoList.begin();
         it != m_vStartMoveInfoList.end(); it ++ ){
        SSceneObjectMovePosInfo* _pSOMovePosInfo = &(*it);
        CSceneObject* _pSO = _pSOMovePosInfo->m_pSceneObject;
        if( _pSO ){
            _pSO->SetPosition( _pSOMovePosInfo->m_sPos.X, _pSOMovePosInfo->m_sPos.Y, _pSOMovePosInfo->m_sPos.Z  );
            list<TPosition> _vPathList1;
            _pSONavigationMap->GetCellTypeListValue( _pSO->m_nCellType, 0, _pSOMovePosInfo->m_sMapPos.X, _pSOMovePosInfo->m_sMapPos.Y, &_vPathList1 );
            _pSONavigationMap->SetSOListValue( &_vPathList1, _pSO );
        }
    }
    return True;
}
bool32 CSceneObjectMoveCmd::Redo(){
    CSceneObjectNavigationMap* _pSONavigationMap = m_pSceneMgr->GetSceneObjectNavigationMap();
    CSceneObjectManager* _pSceneObjectManager = m_pSceneMgr->GetSceneObjectMgr();

    for( list<SSceneObjectMovePosInfo>::iterator it = m_vEndMoveInfoList.begin();
         it != m_vEndMoveInfoList.end(); it ++ ){
        SSceneObjectMovePosInfo* _pSOMovePosInfo = &(*it);
        CSceneObject* _pSO = _pSceneObjectManager->GetSceneObject( _pSOMovePosInfo->m_nID );
        if( _pSO ){
            list<TPosition> _vPathList1;
            _pSONavigationMap->GetCellTypeListValue( _pSO->m_nCellType, 0, _pSO->m_sMapGridPos.X, _pSO->m_sMapGridPos.Y, &_vPathList1 );
            _pSONavigationMap->SetSOListValue( &_vPathList1, NULL );
            _pSOMovePosInfo->m_pSceneObject = _pSO;
        }
    }

    for( list<SSceneObjectMovePosInfo>::iterator it = m_vEndMoveInfoList.begin();
         it != m_vEndMoveInfoList.end(); it ++ ){
        SSceneObjectMovePosInfo* _pSOMovePosInfo = &(*it);
        CSceneObject* _pSO = _pSOMovePosInfo->m_pSceneObject;
        if( _pSO ){
            _pSO->SetPosition( _pSOMovePosInfo->m_sPos.X, _pSOMovePosInfo->m_sPos.Y, _pSOMovePosInfo->m_sPos.Z  );
            list<TPosition> _vPathList1;
            _pSONavigationMap->GetCellTypeListValue( _pSO->m_nCellType, 0, _pSOMovePosInfo->m_sMapPos.X, _pSOMovePosInfo->m_sMapPos.Y, &_vPathList1 );
            _pSONavigationMap->SetSOListValue( &_vPathList1, _pSO );
        }
    }

    return True;
}


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
CSceneObjectRotationCmd::CSceneObjectRotationCmd( list<SSceneObjectRotationScaleInfo> *pvStartInfoList, list<SSceneObjectRotationScaleInfo> *pvEndInfoList ):
CCommand(){
    m_vStartInfoList = *pvStartInfoList;
    m_vEndInfoList = *pvEndInfoList;
}
CSceneObjectRotationCmd::~CSceneObjectRotationCmd(){

}
void CSceneObjectRotationCmd::Init(){

}
void CSceneObjectRotationCmd::Final(){

}
bool32 CSceneObjectRotationCmd::Undo(){
    CSceneObjectManager* _pSceneObjectManager = m_pSceneMgr->GetSceneObjectMgr();
    for( list<SSceneObjectRotationScaleInfo>::iterator it = m_vStartInfoList.begin();
         it != m_vStartInfoList.end(); it ++ ){
        SSceneObjectRotationScaleInfo* _pSORotationScaleInfo = &(*it);
        CSceneObject* _pSO = _pSceneObjectManager->GetSceneObject( _pSORotationScaleInfo->m_nID );
        if( _pSO ){
            _pSO->SetRotation( _pSORotationScaleInfo->m_sValue.X, _pSORotationScaleInfo->m_sValue.Y, _pSORotationScaleInfo->m_sValue.Z );
        }
    }
    return True;
}
bool32 CSceneObjectRotationCmd::Redo(){
    CSceneObjectManager* _pSceneObjectManager = m_pSceneMgr->GetSceneObjectMgr();
    for( list<SSceneObjectRotationScaleInfo>::iterator it = m_vEndInfoList.begin();
         it != m_vEndInfoList.end(); it ++ ){
        SSceneObjectRotationScaleInfo* _pSORotationScaleInfo = &(*it);
        CSceneObject* _pSO = _pSceneObjectManager->GetSceneObject( _pSORotationScaleInfo->m_nID );
        if( _pSO ){
            _pSO->SetRotation( _pSORotationScaleInfo->m_sValue.X, _pSORotationScaleInfo->m_sValue.Y, _pSORotationScaleInfo->m_sValue.Z );
        }
    }
    return True;
}


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
CSceneObjectScaleCmd::CSceneObjectScaleCmd( list<SSceneObjectRotationScaleInfo> *pvStartInfoList, list<SSceneObjectRotationScaleInfo> *pvEndInfoList ):
CCommand(){
    m_vStartInfoList = *pvStartInfoList;
    m_vEndInfoList = *pvEndInfoList;
}
CSceneObjectScaleCmd::~CSceneObjectScaleCmd(){

}
void CSceneObjectScaleCmd::Init(){

}
void CSceneObjectScaleCmd::Final(){

}
bool32 CSceneObjectScaleCmd::Undo(){
    CSceneObjectManager* _pSceneObjectManager = m_pSceneMgr->GetSceneObjectMgr();
    for( list<SSceneObjectRotationScaleInfo>::iterator it = m_vStartInfoList.begin();
         it != m_vStartInfoList.end(); it ++ ){
        SSceneObjectRotationScaleInfo* _pSORotationScaleInfo = &(*it);
        CSceneObject* _pSO = _pSceneObjectManager->GetSceneObject( _pSORotationScaleInfo->m_nID );
        if( _pSO ){
            _pSO->SetScale( _pSORotationScaleInfo->m_sValue.X, _pSORotationScaleInfo->m_sValue.Y, _pSORotationScaleInfo->m_sValue.Z );
        }
    }
    return True;
}
bool32 CSceneObjectScaleCmd::Redo(){
    CSceneObjectManager* _pSceneObjectManager = m_pSceneMgr->GetSceneObjectMgr();
    for( list<SSceneObjectRotationScaleInfo>::iterator it = m_vEndInfoList.begin();
         it != m_vEndInfoList.end(); it ++ ){
        SSceneObjectRotationScaleInfo* _pSORotationScaleInfo = &(*it);
        CSceneObject* _pSO = _pSceneObjectManager->GetSceneObject( _pSORotationScaleInfo->m_nID );
        if( _pSO ){
            _pSO->SetScale( _pSORotationScaleInfo->m_sValue.X, _pSORotationScaleInfo->m_sValue.Y, _pSORotationScaleInfo->m_sValue.Z );
        }
    }
    return True;
}